package com.airbat.prize.modules.tool.dal.mysql.codegen;

import com.airbat.prize.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO;
import com.airbat.prize.mybits.mybatis.core.mapper.BaseMapperX;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ToolSchemaColumnMapper extends BaseMapperX<ToolSchemaColumnDO> {

//    default List<ToolSchemaColumnDO> selectListByTableName(String tableSchema, String tableName) {
//        return selectList(new QueryWrapper<ToolSchemaColumnDO>().eq("table_name", tableName)
//                .eq("table_schema", tableSchema)
//                .orderByAsc("ordinal_position"));
//    }

    @Select("select TABLE_NAME, COLUMN_NAME, DATA_TYPE AS COLUMN_TYPE, DATA_LENGTH AS COLUMN_LENGTH, COLUMN_ID AS ORDINAL_POSITION, \n" +
            "(SELECT COMMENTS FROM user_col_comments WHERE COLUMN_NAME = user_tab_columns.COLUMN_NAME AND rownum <= 1) AS COLUMN_COMMENT, \n" +
            "(case when NULLABLE = 'Y' then '1' else '0' end) NULLABLE, \n" +
            "(SELECT case when column_name IS NOT NULL then '1' else '0' end FROM user_cons_columns WHERE COLUMN_NAME = user_tab_columns.COLUMN_NAME AND CONSTRAINT_NAME LIKE '%_PK%' AND rownum <= 1) AS PRIMARY_KEY\n" +
            "from user_tab_columns where TABLE_NAME = #{tableName} order by COLUMN_ID")
    List<ToolSchemaColumnDO> selectListByTableName(@Param("tableSchema") String tableSchema, @Param("tableName") String tableName);

}
